package com.lanou.moving.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.widget.ListView;
import com.lanou.moving.adapter.LocalMusicAdapter;
import com.lanou.moving.entity.LocalMusicInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by 刘琪 on 15/10/30.
 */
public class ScanLocalMusic {


    public List<LocalMusicInfo> getLocalMusicInfos(ContentResolver contentResolver) {
        Cursor cursor = contentResolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        List<LocalMusicInfo> localMusicInfos = new ArrayList<LocalMusicInfo>();
        for (int i = 0; i < cursor.getCount(); i++) {
            //新建一个歌曲对象,将从cursor里读出的信息存放进去,直到取完cursor里面的内容为止.
            LocalMusicInfo localMusicInfo = new LocalMusicInfo();
            cursor.moveToNext();


            long id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media._ID));    //音乐id

            String title = cursor.getString((cursor
                    .getColumnIndex(MediaStore.Audio.Media.TITLE)));//音乐标题

            String artist = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ARTIST));//艺术家

            long duration = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DURATION));//时长

            long size = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.SIZE));    //文件大小

            String url = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));    //文件路径

            String album = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM)); //唱片图片

            long album_id = cursor.getLong(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); //唱片图片ID

            int isMusic = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐

            if (isMusic != 0 && duration / (1000 * 60) >= 1) {        //只把1分钟以上的音乐添加到集合当中
                localMusicInfo.setId(id);
                localMusicInfo.setTitle(title);
                localMusicInfo.setArtist(artist);
                localMusicInfo.setDuration(duration);
                localMusicInfo.setSize(size);
                localMusicInfo.setUrl(url);
                localMusicInfo.setAlbum(album);
                localMusicInfo.setAlbum_id(album_id);
                localMusicInfos.add(localMusicInfo);
            }
        }
        return localMusicInfos;
    }

    public void setListAdpter(Context context, List<LocalMusicInfo> localMusicInfos, ListView mMusicList) {

        List<HashMap<String, String>> mp3list = new ArrayList<HashMap<String, String>>();
        LocalMusicAdapter mAdapter = new LocalMusicAdapter(context, localMusicInfos);
        mMusicList.setAdapter(mAdapter);
    }
}

